//     // high_score: number;
//     // map: any;
// /** 存储类，用于存储需要本地保存的数据 */

import { _decorator, sys } from 'cc';
interface CircleTheCat_GameStorageData {
    mode_3x3: any;
    mode_4x4: any;
    mode_5x5: any;
}

export class CircleTheCat_GameStorage {
    static data: CircleTheCat_GameStorageData = null;
    static defaultValue: CircleTheCat_GameStorageData = null;
    static initData() {
        CircleTheCat_GameStorage.InitDefaultData();

        let dataStr = sys.localStorage.getItem(`CircleTheCat_GameStorage`);
        if (!dataStr || dataStr === "") {
            CircleTheCat_GameStorage.clearData();
        } else {
            CircleTheCat_GameStorage.data = JSON.parse(dataStr);
            CircleTheCat_GameStorage.saveData();
        }

        CircleTheCat_GameStorage.SetEveryData();
    }
    //    // 初始化默认值
    static InitDefaultData() {
        CircleTheCat_GameStorage.defaultValue = {
            mode_3x3: {
                score: 0,
                high_score: 0,
                map: [],
            },
            mode_4x4: {
                score: 0,
                high_score: 0,
                map: [],
            },
            mode_5x5: {
                score: 0,
                high_score: 0,
                map: [],
            },
        };
    }
    static SetEveryData() {
        for (let key in CircleTheCat_GameStorage.defaultValue) {
            let defaultValue = CircleTheCat_GameStorage.defaultValue[key];
            let dataValue = CircleTheCat_GameStorage.data[key];
            // console.log(key, defaultValue, dataValue);
            if (dataValue === null || dataValue === undefined) {
                CircleTheCat_GameStorage.data[key] = defaultValue;
            }
        }
        CircleTheCat_GameStorage.saveData();
    }
    static clearData() {
        CircleTheCat_GameStorage.data = { ...CircleTheCat_GameStorage.defaultValue };
    }
    static saveData() {
        let dataStr = JSON.stringify(CircleTheCat_GameStorage.data);
        sys.localStorage.setItem(`CircleTheCat_GameStorage`, dataStr);
    }
}